﻿using System.Collections.Generic;
using System.Web.Profile;
using TestSystem.DAL.TestSystem;

namespace TestSystem.BLL.TestSystem
{
    public class UserAnswer : BusinessEntity
    {
        #region private fields
        private int _userAnswerID;
        private string _userID;
        private int _quizID;
        private int _testID;
        private int _ID;
        private string _userAnswerBody;
        #endregion

        #region public prop and methods
        public int UserAnswerID
        {
            get { return _userAnswerID; }
            set { _userAnswerID = value; }
        }

        public string UserID
        {
            get { return _userID; }
            set { _userID = value; }
        }

        public int QuizID
        {
            get { return _quizID; }
            set { _quizID = value; }
        }

        public int TestID
        {
            get { return _testID; }
            set { _testID = value; }
        }

        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        public string UserAnswerBody
        {
            get { return _userAnswerBody; }
            set { _userAnswerBody = value; }
        }

        private ProfileBase _user = null;
        public ProfileBase User
        {
            get
            {
                if (_user == null)
                    _user = ProfileBase.Create(this.UserID);
                return _user;
            }
        }

        private Quiz _currentQuiz = null;
        public Quiz CurrentQuiz
        {
            get
            {
                if (_currentQuiz == null)
                    _currentQuiz = Quiz.GetByQuizID(this.QuizID);
                return _currentQuiz;
            }
        }

        private Test _currentTest = null;
        public Test CurrentTest
        {
            get
            {
                if (_currentTest == null)
                    _currentTest = Test.GetByTestID(this.TestID);
                return _currentTest;
            }
        }

        private Answer _userAnswer = null;
        public Answer _UserAnswer
        {
            get
            {
                if (_userAnswer == null)
                    _userAnswer = Answer.GetByAnswerID(this.UserAnswerID);
                return _userAnswer;
            }
        }

        public override void Update()
        {
            UserAnswerDataAccess.Update(this.UserAnswerID, this.UserID, this.QuizID, this.TestID, this.ID, this.UserAnswerBody);
        }

        protected override void Load(System.Data.IDataReader rdr)
        {
            UserAnswerID = (int)rdr["UserAnswerID"];
            TestID = (int)rdr["TestID"];
            UserID = UserAnswerDataAccess.FromDBValue<string>(rdr["UserID"]);
            QuizID = (int)rdr["QuizID"];
            ID = (int)rdr["ID"];
            UserAnswerBody = UserAnswerDataAccess.FromDBValue<string>(rdr["UserAnswerBody"]);
        }
        #endregion

        #region static methods
        public static List<UserAnswer> GetByTestID(int testID)
        {
            return LoadList<UserAnswer>(UserAnswerDataAccess.GetUserAnswersByTestID(testID));
        }

        public static List<UserAnswer> GetByQuizID(int quizID)
        {
            return LoadList<UserAnswer>(UserAnswerDataAccess.GetUserAnswersByQuizID(quizID));
        }

        public static int Add(int userAnswerID, string userID, int quizID, int testID, string userAnswerBody)
        {
            int id = UserAnswerDataAccess.Insert(userAnswerID, userID, quizID, testID, userAnswerBody);
            return id;
        }

        public static void Remove(int ID)
        {
            UserAnswerDataAccess.Delete(ID);
        }

        public static List<UserAnswer> GetAll()
        {
            return LoadList<UserAnswer>(UserAnswerDataAccess.GetAll());
        }

        public static UserAnswer GetByID(int ID)
        {
            return LoadEntity<UserAnswer>(UserAnswerDataAccess.GetByID(ID));
        }
        #endregion
    }
}
